package cn.e4j.modular.crm.service.impl;

import cn.e4j.core.dto.Dtos;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.AppCxt;
import cn.e4j.core.util.DateUtils;
import cn.e4j.core.util.InvokeUtil;
import cn.e4j.modular.crm.entity.GwcaT;
import cn.e4j.modular.crm.entity.GwcbT;
import cn.e4j.modular.crm.entity.GwccT;
import cn.e4j.modular.crm.mapper.GwcaTMapper;
import cn.e4j.modular.crm.service.GwcaTService;
//import cn.e4j.modular.erpApi.impl.T100WebServiceImpl;
import cn.e4j.modular.oa.service.OAAbandonWsService;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeoutException;

/**
 * <p>
 * 销售合同资料表 服务实现类
 * </p>
 *
 * @author miaoyl
 * @since 2019-11-26
 */
@Service
public class GwcaTServiceImpl extends ServiceImpl<GwcaTMapper, GwcaT> implements GwcaTService {
    @Autowired
    OAAbandonWsService oaAbandonWsService;

//    @Autowired
//    T100WebServiceImpl t100WebService;

    /**
     * 获取税种、税率、汇率类型、汇率及客户发货地址
     */
    @Transactional(readOnly = true)
    @Override
    public Dto getGwakData(Dto inDto) {
        //Dto gwakDto = this.baseMapper.getGwakData(inDto);
        Dto gwakDto = InvokeUtil.invokeBeanMethod("gwcaTMapper,getGwakData",inDto);
        //获取客户送货地址
        List<Dto> gwalDto = this.baseMapper.getGwalData(inDto);
        BigDecimal huilv = this.getHuiLv(inDto.getString("ENT"), gwakDto.getString("GWAK057"), gwakDto.getString("GWAK008"));
        gwakDto.put("huilv", huilv);
        gwakDto.put("gwalList", gwalDto);
        return gwakDto;
    }

    /**
     * 获取汇率
     *
     * @return
     */
    private BigDecimal getHuiLv(String ENT, String type, String biZhong) {
        Dto queryDto = Dtos.newDto();
        if ("2".equals(type)) {
            queryDto.put("ENT", ENT);
            queryDto.put("GWEK001", biZhong);
            queryDto.put("GWEK002", DateUtils.getDate("yyyyMM"));
            BigDecimal huiLv = this.baseMapper.getGwek005(queryDto);
            if (huiLv == null) {
                huiLv = new BigDecimal("1");
            }
            return huiLv;
        } else {
            queryDto.put("ENT", ENT);
            queryDto.put("GWEL001", biZhong);
            queryDto.put("GWEL002", DateUtils.getDate("yyyyMMdd"));
            BigDecimal huiLv = this.baseMapper.getGwel005(queryDto);
            if (huiLv == null) {
                huiLv = new BigDecimal("1");
            }
            return huiLv;
        }
    }

    @Override
    @Transactional
    public void addData(List<GwcbT> gwcbTList, List<GwccT> gwccTList, GwcaT gwcaT) {
        int amount =  Integer.valueOf(AppCxt.getParam("amount_digit_"));
        ShiroUser shiroUser = ShiroKit.getUser();
        //根据申请组织获取组织编码
        Dto bizcode = InvokeUtil.invokeBeanMethod("gwcaTMapper,getBizCode", gwcaT.getGWCAMLEGAL());
        //合同明细新增
        for(GwcbT gwcbT : gwcbTList){
            gwcbT.setGWCBENT(shiroUser.getDBC());
            gwcbT.setGWCBDOCNO(gwcaT.getGWCADOCNO());
            gwcbT.setGWCBUSER(shiroUser.getID());
            gwcbT.setGWCBGRUP(shiroUser.getORG_ID());
            gwcbT.setGWCBTIME(DateUtils.formatDate(new Date(),"HH:mm:ss"));
            gwcbT.setGWCBORIG(shiroUser.getORG_ID());
            gwcbT.setGWCBORIU(shiroUser.getID());
            gwcbT.setGWCBDATE(LocalDate.now());
            //计算未税金额
            BigDecimal shuilv = gwcaT.getGWCA050().divide(BigDecimal.valueOf(100));
            BigDecimal GWCB012 = gwcbT.getGWCB010().divide(BigDecimal.ONE.add(shuilv),amount, BigDecimal.ROUND_HALF_UP);
            gwcbT.setGWCB012(GWCB012);
            gwcbT.setGWCB011(gwcbT.getGWCB010().subtract(GWCB012)); //税额
            gwcbT.insert();
        }

        //收款计划新增
        for(GwccT gwccT : gwccTList){
            gwccT.setGWCCENT(shiroUser.getDBC());
            gwccT.setGWCCDOCNO(gwcaT.getGWCADOCNO());
            gwccT.setGWCCUSER(shiroUser.getID());
            gwccT.setGWCCGRUP(shiroUser.getORG_ID());
            gwccT.setGWCCTIME(DateUtils.formatDate(new Date(),"HH:mm:ss"));
            gwccT.setGWCCORIG(shiroUser.getORG_ID());
            gwccT.setGWCCORIU(shiroUser.getID());
            gwccT.setGWCCDATE(LocalDate.now());
            gwccT.insert();
        }

        gwcaT.setGWCAPLANT(bizcode != null ? bizcode.getString("BIZ_CODE") : "");
        gwcaT.setGWCAENT(shiroUser.getDBC());
        gwcaT.setGWCAUSER(shiroUser.getORG_ID());
        gwcaT.setGWCAGRUP(shiroUser.getID());
        gwcaT.setGWCATIME(DateUtils.formatDate(new Date(),"HH:mm:ss"));
        gwcaT.insert();
    }

    @Override
    @Transactional
    public void editData(List<GwcbT> gwcbTList, List<GwccT> gwccTList, GwcaT gwcaT) {
        int amount =  Integer.valueOf(AppCxt.getParam("amount_digit_"));
        ShiroUser shiroUser = ShiroKit.getUser();
        GwcbT gwcbTDel = new GwcbT();
        UpdateWrapper<GwcbT> gwcbTUpdateWrapper = new UpdateWrapper<>();
        gwcbTUpdateWrapper.eq("GWCBENT", gwcaT.getGWCAENT());
        gwcbTUpdateWrapper.eq("GWCBDOCNO", gwcaT.getGWCADOCNO());
        gwcbTDel.delete(gwcbTUpdateWrapper);

        GwccT gwccTDel = new GwccT();
        UpdateWrapper<GwccT> gwccTUpdateWrapper = new UpdateWrapper<>();
        gwccTUpdateWrapper.eq("GWCCENT", gwcaT.getGWCAENT());
        gwccTUpdateWrapper.eq("GWCCDOCNO", gwcaT.getGWCADOCNO());
        gwccTDel.delete(gwccTUpdateWrapper);

        //合同明细新增
        for(GwcbT gwcbT : gwcbTList){
            gwcbT.setGWCBENT(shiroUser.getDBC());
            gwcbT.setGWCBDOCNO(gwcaT.getGWCADOCNO());
            gwcbT.setGWCBUSER(shiroUser.getID());
            gwcbT.setGWCBGRUP(shiroUser.getORG_ID());
            gwcbT.setGWCBTIME(DateUtils.formatDate(new Date(),"HH:mm:ss"));
            gwcbT.setGWCBORIG(shiroUser.getORG_ID());
            gwcbT.setGWCBORIU(shiroUser.getID());
            gwcbT.setGWCBDATE(LocalDate.now());
            //计算未税金额
            BigDecimal shuilv = gwcaT.getGWCA050().divide(BigDecimal.valueOf(100));
            BigDecimal GWCB012 = gwcbT.getGWCB010().divide(BigDecimal.ONE.add(shuilv),amount, BigDecimal.ROUND_HALF_UP);
            gwcbT.setGWCB012(GWCB012);
            gwcbT.setGWCB011(gwcbT.getGWCB010().subtract(GWCB012)); //税额
            gwcbT.setGWCBMODU(shiroUser.getID());
            gwcbT.insert();
        }

        //收款计划新增
        for(GwccT gwccT : gwccTList){
            gwccT.setGWCCENT(shiroUser.getDBC());
            gwccT.setGWCCDOCNO(gwcaT.getGWCADOCNO());
            gwccT.setGWCCUSER(shiroUser.getID());
            gwccT.setGWCCGRUP(shiroUser.getORG_ID());
            gwccT.setGWCCTIME(DateUtils.formatDate(new Date(),"HH:mm:ss"));
            gwccT.setGWCCORIG(shiroUser.getORG_ID());
            gwccT.setGWCCORIU(shiroUser.getID());
            gwccT.setGWCCDATE(LocalDate.now());
            gwccT.setGWCCMODU(shiroUser.getID());
            gwccT.insert();
        }
        //根据申请组织获取组织编码
        Dto bizcode = InvokeUtil.invokeBeanMethod("gwcaTMapper,getBizCode", gwcaT.getGWCAMLEGAL());
        gwcaT.setGWCAPLANT(bizcode != null ? bizcode.getString("BIZ_CODE") : "");
        gwcaT.setGWCAMODU(shiroUser.getID());
        UpdateWrapper<GwcaT> gwcaTUpdateWrapper = new UpdateWrapper<>();
        gwcaTUpdateWrapper.eq("GWCAENT", gwcaT.getGWCAENT());
        gwcaTUpdateWrapper.eq("GWCADOCNO", gwcaT.getGWCADOCNO());
        gwcaT.update(gwcaTUpdateWrapper);
    }

    @Override
    @Transactional
    public void deleteData(GwcaT gwcaT) {
        GwcbT gwcbTDel = new GwcbT();
        UpdateWrapper<GwcbT> gwcbTUpdateWrapper = new UpdateWrapper<>();
        gwcbTUpdateWrapper.eq("GWCBENT", gwcaT.getGWCAENT());
        gwcbTUpdateWrapper.eq("GWCBDOCNO", gwcaT.getGWCADOCNO());

        GwccT gwccTDel = new GwccT();
        UpdateWrapper<GwccT> gwccTUpdateWrapper = new UpdateWrapper<>();
        gwccTUpdateWrapper.eq("GWCCENT", gwcaT.getGWCAENT());
        gwccTUpdateWrapper.eq("GWCCDOCNO", gwcaT.getGWCADOCNO());

        UpdateWrapper<GwcaT> gwcaTUpdateWrapper = new UpdateWrapper<>();
        gwcaTUpdateWrapper.eq("GWCAENT", gwcaT.getGWCAENT());
        gwcaTUpdateWrapper.eq("GWCADOCNO", gwcaT.getGWCADOCNO());

        gwcbTDel.delete(gwcbTUpdateWrapper);
        gwccTDel.delete(gwccTUpdateWrapper);
        gwcaT.delete(gwcaTUpdateWrapper);
    }

    @Override
    @Transactional
    public void delDataBatch(List<GwcaT> gwcaTList) {
        for(GwcaT gwcaT : gwcaTList) {
            GwcbT gwcbTDel = new GwcbT();
            UpdateWrapper<GwcbT> gwcbTUpdateWrapper = new UpdateWrapper<>();
            gwcbTUpdateWrapper.eq("GWCBENT", gwcaT.getGWCAENT());
            gwcbTUpdateWrapper.eq("GWCBDOCNO", gwcaT.getGWCADOCNO());

            GwccT gwccTDel = new GwccT();
            UpdateWrapper<GwccT> gwccTUpdateWrapper = new UpdateWrapper<>();
            gwccTUpdateWrapper.eq("GWCCENT", gwcaT.getGWCAENT());
            gwccTUpdateWrapper.eq("GWCCDOCNO", gwcaT.getGWCADOCNO());

            UpdateWrapper<GwcaT> gwcaTUpdateWrapper = new UpdateWrapper<>();
            gwcaTUpdateWrapper.eq("GWCAENT", gwcaT.getGWCAENT());
            gwcaTUpdateWrapper.eq("GWCADOCNO", gwcaT.getGWCADOCNO());

            gwcbTDel.delete(gwcbTUpdateWrapper);
            gwccTDel.delete(gwccTUpdateWrapper);
            gwcaT.delete(gwcaTUpdateWrapper);
        }
    }

    @Override
    public List<Map<String, Object>> getGwcbData(Dto dto) {
        List<Map<String, Object>> gwcbDtos = this.baseMapper.getGwcbData(dto);
        return gwcbDtos;
    }

    @Override
    public List<Map<String, Object>> getGwccData(Dto dto) {
        //List<Map<String, Object>> gwccDtos = this.baseMapper.getGwccData(dto);
        List<Map<String, Object>> gwccDtos = InvokeUtil.invokeBeanMethod("gwcaTMapper,getGwccData", dto);
        return gwccDtos;
    }

    @Override
    public List<Map<String, Object>> getGwcbList(Page<Object> page, Dto dto) {
        List<Map<String, Object>> gwcbDtos = this.baseMapper.getGwcbList(page, dto);
        return gwcbDtos;
    }

    @Override
    public List<Map<String, Object>> getGwccList(Page<Object> page, Dto dto) {
        List<Map<String, Object>> gwccDtos = InvokeUtil.invokeBeanMethod("gwcaTMapper,getGwccList",page, dto);
        return gwccDtos;
    }

    @Transactional
    @Override
    public Dto updateStatus(List<GwcaT> gwcaTList) {
        String state = gwcaTList.get(0).getGWCAACTI();
        Dto outDto = Dtos.newDto();
        for (GwcaT gwcaT : gwcaTList) {
            GwcaT updateGwcaT = new GwcaT();
            updateGwcaT.setGWCAACTI(gwcaT.getGWCAACTI());
            UpdateWrapper<GwcaT> gwcaTUpdateWrapper = new UpdateWrapper<>();
            gwcaTUpdateWrapper.eq("GWCAENT", gwcaT.getGWCAENT());
            gwcaTUpdateWrapper.eq("GWCADOCNO", gwcaT.getGWCADOCNO());
            updateGwcaT.update(gwcaTUpdateWrapper);
        }
        outDto.setAppCode(200);
        return outDto;
    }

    @Transactional(propagation = Propagation.NESTED)
    public String createOrder(String gwcadocno, String type){
//        String doc_no = "";
//        Dto inDto = Dtos.newDto();
//        inDto.put("GWCADOCNO", gwcadocno);
//        inDto.put("ENT", AppCxt.getParam("ENT"));
//        inDto.put("LANG", "2");
//        if("3".equals(type)){
//            Dto gwcaData = this.baseMapper.getGwcaData(inDto);
//            List<Map<String, Object>> gwcbData = this.baseMapper.getGwcbData(inDto);
//            List<Map<String, Object>> gwccData = this.baseMapper.getGwccData(inDto);
//            doc_no = t100WebService.createOrder(gwcaData, gwcbData, gwccData, AppCxt.getParam("ERP_ENT"));
//        }else if("4".equals(type)){
//            Dto gwcaData = this.baseMapper.getGwcaData(inDto);
//            doc_no = t100WebService.updateOrder(gwcaData, AppCxt.getParam("ERP_ENT"));
//        }
//        GwcaT gwcaT = new GwcaT();
//        gwcaT.setGWCA054(doc_no);
//        UpdateWrapper<GwcaT> gwcaTUpdateWrapper = new UpdateWrapper<>();
//        gwcaTUpdateWrapper.eq("GWCADOCNO", gwcadocno);
//        gwcaT.update(gwcaTUpdateWrapper);
//        return doc_no;
        return null;
    }

    @Override
    public List<Dto> getBookmarkData(Page page, Dto inDto) {
        return InvokeUtil.invokeBeanMethod("gwcaTMapper,getBookmarkData", page, inDto);
    }

}
